package org.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import java.io.IOException;
/**
 * MapReduce示例程序
 */
//步1：开发一个类，继承Configured实现接口Tool
public class Demo05MapReduce extends Configured implements Tool {
    //步3:添加main函数
    public static void main(String[] args) throws Exception {
        //步7：开始任务
        System.setProperty("HADOOP_HOME", "D:/program/hadoop-3.2.2");
        System.setProperty("hadoop.home.dir", "D:/program/hadoop-3.2.2");
        int res = ToolRunner.run(new Demo05MapReduce(),args);
        System.exit(res);
    }
    //步2：实现run函数
    @Override
    public int run(String[] strings) throws Exception {
        //步6：开发run函数内部代码
        Configuration conf =getConf();
        Job job = Job.getInstance(conf,"WordCount");
        job.setJarByClass(getClass());
        FileSystem fs = FileSystem.get(conf);
        //声明输出目录
        Path dest = new Path("D:/a/out");
        //如果输出目录已存在则删除
        if(fs.exists(dest)){
            fs.delete(dest,true);
        }
        //设置Mapper及Mapper输出的类型
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //设置Reduce及Reduce的输出类型
        job.setReducerClass(MyReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //设置输入和输出类型
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        //设置输入输出目录
        FileInputFormat.addInputPath(job,new Path("D:/a/a.txt"));
        FileOutputFormat.setOutputPath(job,dest);
        //开始执行任务
        boolean boo = job.waitForCompletion(true);
        return boo?0:1;
    }
    //步4：开发Mapper类的实现类
    public static class MyMapper extends Mapper<LongWritable, Text,Text,LongWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            Text outKey = new Text();
            LongWritable outValue = new LongWritable(1L);
            if(value.getLength()>0){
                String[] strs = value.toString().split("\\s+");
                for(String str:strs){
                    outKey.set(str);
                    context.write(outKey,outValue);
                }
            }
        }
    }
    //步5：开发Reduce程序
    public static class MyReduce extends Reducer<Text,LongWritable,Text,LongWritable>{
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            long sum = 0;
            LongWritable resultValue = new LongWritable(0);
            for(LongWritable v:values){
                sum+=v.get();
            }
            resultValue.set(sum);
            context.write(key,resultValue);
        }
    }
}
